import java.util.Arrays;

public class Leetcode891 {
    public static void main(String[] args) {
        System.out.println(sumSubseqWidths(new int[]{5, 69, 89, 92, 31, 16, 25, 45, 63, 40, 16, 56, 24, 40, 75, 82, 40, 12, 50, 62, 92, 44, 67, 38, 92, 22, 91, 24, 26, 21, 100, 42, 23, 56, 64, 43, 95, 76, 84, 79, 89, 4, 16, 94, 16, 77, 92, 9, 30, 13}));
        System.out.println(sumSubseqWidths(new int[]{1, 3, 1, 6, 2, 2, 7}));
        System.out.println(sumSubseqWidths(new int[]{2, 1, 3}));
    }

    private static final int MOD = 1000000007;

    private static long[] twoPow;

    public static int sumSubseqWidths(int[] nums) {
        twoPow = new long[nums.length];
        twoPow[0] = 1;
        for (int i = 1; i < nums.length; i++) {
            twoPow[i] = (twoPow[i - 1] * 2) % MOD;
        }

        Arrays.sort(nums);
        double res = 0;
        for (int i = 0; i < nums.length; i++) {
            res = (res + ((twoPow[i] * nums[i] - twoPow[nums.length - i - 1] * nums[i]) % MOD)) % MOD;
        }

        return (int) res;
    }
}

